// LGUIContainer.cpp: implementation of the LGUIContainer class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LGUI.h"
#include "LGUIContainer.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

LGUIContainer::LGUIContainer(int _x, int _y, int _w, int _h) : LGUIWindow(_x,_y,_w,_h)
{
	firstChild=lastChild=0;
	clientX=clientY=0;
}



LGUIContainer::~LGUIContainer()
{
	destroyChildren();

}



void LGUIContainer::add(LGUIWindow *wnd)
{
	if (wnd->parent)
	{
		wnd->parent->remove(wnd);
	}
	if (firstChild)
	{
		lastChild->next=wnd;
	}
	else
	{
		firstChild=wnd;
	}
	lastChild = wnd;
	wnd->next=0;
	wnd->parent = this;
}



bool LGUIContainer::remove(LGUIWindow *wnd)
{
	LGUIWindow *w = firstChild, *prev;
	if (w==wnd)
	{
		firstChild = wnd->next;
		wnd->next = 0;
		wnd->parent = 0;
		LGUIWindow *w = firstChild;

		if (w)
		{
			while (w->next) w=w->next;
			lastChild=w;
		}
		else
			lastChild=firstChild; //0

		return true;
	}
	prev=w;
	w = w->next;
	while (w)
	{
		if (w==wnd)
		{
			prev->next=wnd->next;
			wnd->next = 0;
			wnd->parent = 0;
			return true;
		}
		w = w->next;
	}
	w=firstChild;
	if (w)
	{
		while (w->next) w=w->next;
		lastChild=w;
	}
	else
		lastChild=firstChild; //0

	return false;
}



void LGUIContainer::destroyChildren()
{
	LGUIWindow *w = firstChild;
	firstChild = 0;
	while (w)
	{
		LGUIWindow *tmp = w->next;
		delete w;
		w=tmp;
	}
}



void LGUIContainer::bringChildToTop(LGUIWindow *wnd)
{
	if (wnd == 0 || lastChild == wnd)  
		return;

	if (remove(wnd))
		add(wnd);
}


void LGUIContainer::penDown(int px, int py)
{
	LGUIWindow *w=firstChild;
	while (w)
	{
		if (w->isVisible) 
			w->penDown(px-x,py-y);
		w=w->next;
	}
}



void LGUIContainer::penMove(int px, int py, bool pressed)
{
	LGUIWindow *w=firstChild;
	while (w)
	{
		if (w->isVisible) 
			w->penMove(px-x,py-y,pressed);
		w=w->next;
	}
}



void LGUIContainer::penUp(int px, int py)
{
	LGUIWindow *w=firstChild;
	while (w)
	{
		if (w->isVisible) 
			w->penUp(px-x,py-y);
		w=w->next;
	}
}



void LGUIContainer::keyDown(int key)
{
	LGUIWindow *w=firstChild;
	while (w)
	{
		if (w->isVisible) 
			w->keyDown(key);
		w=w->next;
	}
}

void LGUIContainer::keyChar(int key)
{
	LGUIWindow *w=firstChild;
	while (w)
	{
		if (w->isVisible) 
			w->keyChar(key);
		w=w->next;
	}
}



void LGUIContainer::keyUp(int key)
{
	LGUIWindow *w=firstChild;
	while (w)
	{
		if (w->isVisible) 
			w->keyUp(key);
		w=w->next;
	}
}


void LGUIContainer::draw(int xoff, int yoff)
{
	LGUIWindow *w=firstChild;
	while (w)
	{
		if (w->isVisible) 
			w->draw(x+xoff,y+yoff);
		w=w->next;
	}
}


void LGUIContainer::redraw()
{
	LGUIWindow *w=firstChild;
	while (w)
	{
		if (w->isVisible) 
			w->redraw();
		w=w->next;
	}

	LGUIWindow::redraw();
}


LGUIWindow *LGUIContainer::findChildWithUserData(int u)
{
	LGUIWindow *w=firstChild;
	while (w)
	{
		if (w->isContainer())
		{
			LGUIWindow *res = ((LGUIContainer *)w)->findChildWithUserData(u);
			if (res)
				return res;
		}
		else if (w->getUserData()==u)
			return w;

		w=w->next;
	}

	return 0;
}

std::vector<LGUIWindow *> LGUIContainer::findChildrenWithUserData(int u)
{
	std::vector<LGUIWindow *> foundChildren;
	LGUIWindow *w=firstChild;
	while (w)
	{
		if (w->isContainer())
		{
			std::vector<LGUIWindow *> children = ((LGUIContainer *)w)->findChildrenWithUserData(u);
			foundChildren.insert(foundChildren.end(), children.begin(), children.end());
		}
		else if (w->getUserData()==u)
		{
			foundChildren.push_back(w);
		}

		w=w->next;
	}

	return foundChildren;
}

LGUIWindow *LGUIContainer::findChildWithID(int id)
{
	LGUIWindow *w=firstChild;
	while (w)
	{
		if (w->isContainer())
		{
			LGUIWindow *res = ((LGUIContainer *)w)->findChildWithID(id);
			if (res)
				return res;
		}
		else if (w->getID() == id)
			return w;

		w=w->next;
	}

	return 0;
}
